<!--
 * @Description: 
 * @Author: 胡涛
 * @Date: 2023-10-12 23:42:17
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-10-12 23:57:27
-->
<template>
  <a-modal
    :open="visible"
    :keyboard="false"
    :maskClosable="false"
    :title="title"
    @cancel="onCancel"
    @ok="onSubmit"
    :confirmLoading="submitLoading"
    :width="600"
  >
    <a-form ref="formRef" v-bind="formLayout" :model="model" :rules="rules">
      <a-form-item label="资源名" prop="id">
        <a-input v-model:value="model.id" :disabled="isEdit"></a-input>
      </a-form-item>
      <a-form-item label="资源块描述" prop="description">
        <a-input v-model:value="model.description"></a-input>
      </a-form-item>
      <a-form-item label="资源权限" prop="isPublic">
        <a-radio-group v-model:value="model.isPublic">
          <a-radio :value="false">私用</a-radio>
          <a-radio :value="true">共有</a-radio>
        </a-radio-group>
      </a-form-item>

      <a-form-item label="状态" prop="status">
        <a-checkbox v-model:checked="model.status">启用/停用</a-checkbox>>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { BucketDto } from "@/api/resource/types";
import { useFormPage } from "@/helpers/pages";
import { FormInstance } from "ant-design-vue";
import { computed, onMounted, ref, watch } from "vue";

import api from "@/api/resource/bucket";
const formRef = ref<FormInstance>();
const emits = defineEmits(["update:visible", "close", "success"]);
const props = defineProps({
  id: String,
  visible: { type: Boolean, default: false },
  action: {
    type: String,
    default: "create",
  },
  item: {
    type: Object,
    default() {
      return {};
    },
  },
});

const { model, rules, formLayout, loading, submitLoading } = useFormPage<
  BucketDto,
  string
>({
  rules: {
    id: [{ required: true, message: "资源块名称不允许为空" }],
  },
  model: {
    id: "",
    status: true,
    isPublic: false,
    description: "",
  },
});

const isEdit = computed(() => {
  return props.action === "edit";
});

/**
 * 弹窗标题
 */
const title = computed(() => {
  return isEdit.value ? "修改资源块信息" : "添加资源块信息";
});

/**
 * 加载数据
 */
const loadData = () => {
  if (isEdit.value) {
    loading.value = true;
    api.get(props.id).then((res) => {
      Object.assign(model, res);
      loading.value = false;
    });
  }
};

/**
 * 提交
 */
const onSubmit = () => {
  emits("success");
  formRef.value.validate().then(() => {
    let request = isEdit.value
      ? api.update(props.id, model)
      : api.create(model);
    submitLoading.value = true;
    request.then((res) => {
      emits("success", res);
      emits("update:visible", false);
      emits("close");
    })
      .finally(() => {
        submitLoading.value = false;
      });
  });
};

/**
 * 取消
 */
const onCancel = () => {
  emits("update:visible", false);
  emits("close");
};

watch(
  () => props.visible,
  (val: boolean) => {
    console.log("visible changed,val,", val);
    val && loadData();
  },
  { immediate: true }
);
</script>

<style lang="less" scoped></style>
